package com.mlamp.nanchang.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mlamp.nanchang.entity.CamerasRequest;
import com.mlamp.nanchang.entity.RlsbBak;
import com.mlamp.nanchang.hikvision.HkArtemisConfig;
import com.mlamp.nanchang.hikvision.HkArtemisHttpUtil;
import com.mlamp.nanchang.mapper.RlsbBakMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RlsbBakService {
	private static final Logger log = LoggerFactory.getLogger(RlsbBakService.class);
	@Autowired
	private RlsbBakMapper rlsbBakMapper;

	private static final String ARTEMIS_PATH = "/artemis";
//	private static final HkArtemisHttpUtil artemisHttpUtil = new HkArtemisHttpUtil(new HkArtemisConfig("47.49.21.159", "26864254", "oC2JAzu6PH5OqqcbGQCy"));
	private static final HkArtemisHttpUtil artemisHttpUtil = new HkArtemisHttpUtil(new HkArtemisConfig("47.49.21.159", "20989624", "pbctiWR4faouT8r9QkHV"));


	//查询后立即保存
//	@Async
	@Transactional
	public void pagingAndSave(final int pageNo, final int pageSize) {
		log.debug("第{}页", pageNo);
		Page<RlsbBak> page = pagingCameras(pageNo, pageSize);
		if(page != null) {
			saveCameras(page);
		}
	}

	// 分页获取监控点资源
	public Page<RlsbBak> pagingCameras(int pageNo, int pageSize) {
		String camerasDataApi = ARTEMIS_PATH + "/api/resource/v1/cameras";
		Map<String, String> path = new HashMap<String, String>(2) {
			{
				put("https://", camerasDataApi);
			}
		};
		CamerasRequest camerasRequest = new CamerasRequest(pageNo, pageSize);
		String body = JSON.toJSONString(camerasRequest);
		String result = artemisHttpUtil.doPostStringArtemis(path, body, null, null, "application/json");
		//log.debug("接口请求结果: {}", result);
		JSONObject json = (JSONObject) JSON.parse(result);
		String code = json.getString("code");
		if("0".equals(code)) {
			Page<RlsbBak> page = new Page<>();
			//成功
        	JSONObject data = json.getJSONObject("data");
        	JSONArray list = data.getJSONArray("list");
        	List<RlsbBak> rlsbList = this.transformData(list);
        	page.setTotal(data.getLongValue("total"));
        	page.setRecords(rlsbList);
        	return page;
		} else {
			log.warn("请求第{}页失败 ==> {}", pageNo, result);
		}
		return null;
	}
	
	@Transactional
	public void saveCameras(Page<RlsbBak> page) {
		for (RlsbBak rlsb : page.getRecords()) {
			if(rlsbBakMapper.selectById(rlsb.getCameraIndexCode()) != null) {
//				rlsbBakMapper.updateById(rlsb);
			} else {
				this.rlsbBakMapper.insert(rlsb);
			}
		}
	}

	public List<RlsbBak> transformData(JSONArray array) {
		List<RlsbBak> list = new ArrayList<>();
		for (Object object : array) {
			JSONObject json = (JSONObject) object;
			RlsbBak rlsb = new RlsbBak();
			rlsb.setCameraIndexCode(json.getString("cameraIndexCode"));
			rlsb.setName(json.getString("name"));
			rlsb.setUnitIndexCode(json.getString("unitIndexCode"));
			rlsb.setGbIndexCode(json.getString("gbIndexCode"));
			rlsb.setDeviceIndexCode(json.getString("deviceIndexCode"));
			rlsb.setLatitude(json.getString("latitude"));
			rlsb.setLongitude(json.getString("longitude"));
			rlsb.setAltitude(json.getString("altitude"));
			rlsb.setPixel(json.getString("pixel"));
			rlsb.setCameraType(json.getString("cameraType"));
			rlsb.setInstallPlace(json.getString("installPlace"));
			rlsb.setMatrixCode(json.getString("matrixCode"));
			rlsb.setChanNum(json.getString("chanNum"));
			rlsb.setViewshed(json.getString("viewshed"));
			rlsb.setCapabilitySet(json.getString("capabilitySet"));
			rlsb.setIntelligentSet(json.getString("intelligentSet"));
			rlsb.setRecordLocation(json.getString("recordLocation"));
			rlsb.setPtzController(json.getString("ptzController"));
			rlsb.setDeviceResourceType(json.getString("deviceResourceType"));
			rlsb.setChannelType(json.getString("channelType"));
			rlsb.setTransType(json.getString("transType"));
			rlsb.setUpdateTime(json.getString("updateTime"));
			rlsb.setCreateTime(json.getString("createTime"));
			rlsb.setStatus(json.getString("status"));
			rlsb.setCameraTypeName(json.getString("cameraTypeName"));
			rlsb.setCapabilitySetName(json.getString("capabilitySetName"));
			rlsb.setIntelligentSetName(json.getString("intelligentSetName"));
			rlsb.setRecordLocationName(json.getString("recordLocationName"));
			rlsb.setPtzControllerName(json.getString("ptzControllerName"));
			rlsb.setDeviceResourceTypeName(json.getString("deviceResourceTypeName"));
			rlsb.setChannelTypeName(json.getString("channelTypeName"));
			rlsb.setTransTypeName(json.getString("transTypeName"));
			rlsb.setTreatyType(json.getString("treatyType"));
			rlsb.setTreatyTypeName(json.getString("treatyTypeName"));
			rlsb.setStatusName(json.getString("statusName"));
			list.add(rlsb);
		}
		return list;
	}
}
